package rd.android.hardcore.ui.pad 
{
	import aze.motion.eaze;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.events.TouchEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.DropShadowFilter;
	import flash.geom.Rectangle;
	import flash.utils.setTimeout;
	import flash.utils.Timer;
	import rd.android.hardcore.datas.Track;
	import rd.android.hardcore.event.PadsEvent;
	import rd.android.hardcore.event.SoundSystemEvent;
	import rd.android.hardcore.params.GlobalParams;
	import rd.android.hardcore.ui.pad.PadMask;

	
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class Pad extends Sprite
	{
		private var display			: Rectangle;
		private var front			: FrontPad;
		
		private var shadow			: DropShadowFilter;
		private var _mask			: PadMask;
		private var track			: Track;
		private var timerRelease	: Timer;
		private var isRelease		: Boolean = false;
		
		public function Pad(node : XML) 
		{
			track = new Track();
			track.build(node);
			
			display = new Rectangle(0, 0, GlobalParams.PAD_SIZE, GlobalParams.PAD_SIZE);
			scrollRect = display;
			
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			front = new FrontPad();
			front.setThumb(track.thumb);
			addChild(front);
			
			timerRelease = new Timer(750);
			
			front.addEventListener(MouseEvent.CLICK, handleClick);
			front.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseReleaseDown);
			
			shadow = new DropShadowFilter(2, 90, 0, .8, 5, 5, 1, 3);
			filters = [shadow];
			
			_mask = new PadMask();
			addChild(_mask);
			front.mask = _mask;
			
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			removeChild(front);
			
			front.removeEventListener(MouseEvent.CLICK, handleClick);
			front.removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseReleaseDown);
			stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseReleaseUp);
			
			filters = null;
			
			removeChild(_mask);
		}
		
		private function handleMouseReleaseUp(e:MouseEvent):void 
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, handleMouseReleaseUp);
			trace("3: MOUSE UP!!!");
			if (timerRelease.running) {
				timerRelease.stop();
				timerRelease.removeEventListener(TimerEvent.TIMER, handleReleaseOK);
			}
			isRelease = false;
		}
		
		private function handleReleaseOK(e:TimerEvent):void 
		{
			trace("4: RELEASEOK");
			if (timerRelease.running) {
				timerRelease.stop();
				timerRelease.removeEventListener(TimerEvent.TIMER, handleReleaseOK);
			}
			if (isRelease) {
				dispatchEvent(new PadsEvent(PadsEvent.PAD_IS_RELEASED, track.keyId, true));
			}
		}
		
		private function handleMouseReleaseDown(e:MouseEvent):void 
		{
			stage.addEventListener(MouseEvent.MOUSE_UP, handleMouseReleaseUp);
			
			isRelease = true;
			timerRelease = new Timer(750);
			timerRelease.addEventListener(TimerEvent.TIMER, handleReleaseOK);
			
			timerRelease.start();
		}
		
		private function handleClick(e:MouseEvent):void 
		{
			registerSound();
		}
		
		private function registerSound():void
		{
			dispatchEvent(	new SoundSystemEvent(SoundSystemEvent.PLAY_SOUND, track, true )	);

			eaze(front).from(.3).filter(ColorMatrixFilter, { brightness: .5 } );
			
			setTimeout(cleanRoll, 400);
		}
		
		private function cleanRoll():void
		{
			front.filters = null;
		}
		
		public function resize(size:int):void
		{
			_mask.height = _mask.width = size;
		}
		
	}

}